package com.xiake.app.web.resolver;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import com.xiake.core.annotation.LoginUser;
import com.xiake.core.model.AppUserInfo;
import com.xiake.service.user.UserInfoService;

public class LoginUserMethodArgumentResolver implements
		HandlerMethodArgumentResolver {
	@Autowired
	private UserInfoService userInfoService;

	@Override
	public Object resolveArgument(MethodParameter parameter,
			ModelAndViewContainer mvc, NativeWebRequest webRequest,
			WebDataBinderFactory binderFactory) throws Exception {
		String token = webRequest.getHeader("token");
		AppUserInfo appUser = userInfoService.getAppUserInfoByToken(token);
		return appUser;
	}

	@Override
	public boolean supportsParameter(MethodParameter parameter) {
		if (parameter.hasParameterAnnotation(LoginUser.class)) {
			return true;
		}
		return false;
	}
}
